﻿using Microsoft.AspNetCore.Mvc;
using WorldmeServer.DTO.游戏相关;
using WorldmeServer.DTO.用户相关;
using WorldmeServer.Models.用户相关;
using WorldmeServer.Services.Business.游戏相关;
using WorldmeServer.Services.Business.用户相关;

namespace WorldmeServer.Controllers.游戏相关
{
    [ApiController]
    [Route("api/players")]
    public class PlayerController : ControllerBase
    {
        private readonly PlayerService playerService;
        public PlayerController(PlayerService playerService)
        {
            this.playerService = playerService;
        }

        [HttpGet]
        [Route("get-player/{id}")]
        public async Task<IActionResult> GetPlayer(string id)
        {
            var result = await playerService.GetPlayer(id);
            if (!result.IsSuccess)
                return BadRequest(result.Message);
            return Ok(result.Data);
        }

        [HttpGet]
        [Route("get-players/{userId}")]
        public async Task<IActionResult> GetPlayers(string userId)
        {
            var result = await playerService.GetPlayerList(userId);
            if (!result.IsSuccess)
                return BadRequest(result.Message);
            return Ok(result.Data);
        }

        [HttpPost]
        [Route("create")]
        public async Task<IActionResult> Create([FromBody] CreatePlayerDto dto)
        {
            var user = HttpContext.Items["User"] as User;
            var result = await playerService.CreatePlayer(user.Id, dto);
            if (!result.IsSuccess)
                return BadRequest(result.Message);
            return Ok(result.Data);
        }

        [HttpDelete]
        [Route("delete/{id}")]
        public async Task<IActionResult> Delete(string id)
        {
            var user = HttpContext.Items["User"] as User;
            var result = await playerService.DeletePlayer(user.Id, id);
            if (!result.IsSuccess)
                return BadRequest(result.Message);
            return Ok(result.Data);
        }

        [HttpPut]
        [Route("update")]
        public async Task<IActionResult> Update(UpdatePlayerDto dto)
        {
            var user = HttpContext.Items["User"] as User;
            var result = await playerService.UpdatePlayer(user.Id, dto);
            if (!result.IsSuccess)
                return BadRequest(result.Message);
            return Ok(result.Data);
        }
    }
}
